package com.maxd.respository.article;


import com.maxd.model.Article;
import com.maxd.model.UserInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface ArticleRepository extends Repository<Article, Long> {


    @Query(value = "select * from article where id=:articleId",
    nativeQuery = true)
    Article selectArticleById(@Param("articleId") Long articleId);

    @Query(value = "select * from article where classify_id=:classifyId and state=:state",
    countQuery = "select count(*) from article where classify_id=:classifyId and state=:state",
    nativeQuery = true)
    Page<Article> selectArticleList(@Param("classifyId") Long classifyId, @Param("state") Integer state, Pageable pageable);

    @Transactional
    @Modifying
    @Query(value = "update article set state=:state where id=:articleId",nativeQuery = true)
    Integer updateArticleStateById(@Param("articleId") Long articleId, @Param("state") Integer state);

    @Query(value = "from Article s where s.classifyId=:classifyId order by id desc")
    List<Article> getArticleList(@Param("classifyId") Long classifyId);
}